package guestbook;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;

import datebase.DatabaseTest;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Input extends JInternalFrame {
	private JLabel lblNewLabel;
	private JTextField ID;
	private JButton btnNewButton;
	private JScrollPane scrollPane;
	private JLabel label;
	private JTextField tit;
	private JLabel label_1;
	private JTextField pwd;
	private JLabel lblNewLabel_1;
	private JTextPane result;

	Connection con;
	GuestBookMain gbm;
	
	
	/**
	 * Create the frame.
	 */
	public Input(Connection c, GuestBookMain o) {
		super("input", true, true, true, true);
		this.con = c;
		this.gbm = o;
		
		setTitle("방명록 입력");
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(null);
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getID());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getScrollPane());
		getContentPane().add(getLabel());
		getContentPane().add(getTit());
		getContentPane().add(getLabel_1());
		getContentPane().add(getPwd());
		getContentPane().add(getLblNewLabel_1());

	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("닉네임 :");
			lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
			lblNewLabel.setBounds(33, 13, 57, 15);
		}
		return lblNewLabel;
	}
	private JTextField getID() {
		if (ID == null) {
			ID = new JTextField();
			ID.setBounds(89, 10, 79, 21);
			ID.setColumns(10);
		}
		return ID;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("입력");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try {
						String c = ID.getText();
						String a = tit.getText();
						String b = result.getText();
						String sql = "insert into guestbook(serial, mid, subject, content) values(gb_serial.nextval,?,?,?)";
						PreparedStatement pstmt = con.prepareStatement(sql);
						pstmt.setString(1, c);
						pstmt.setString(2, a);
						pstmt.setString(3, b);
						
						pstmt.executeUpdate();
						ID.setText("");
						tit.setText("");
						result.setText("");
						
						if (gbm.select !=null)
							gbm.select.setData();
					} catch (SQLException e1) {
						e1.printStackTrace();
					}
				}
			});
			btnNewButton.setBounds(325, 9, 97, 23);
		}
		return btnNewButton;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 76, 410, 185);
			scrollPane.setViewportView(getResult());
		}
		return scrollPane;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("제 목 :");
			label.setHorizontalAlignment(SwingConstants.CENTER);
			label.setBounds(33, 42, 57, 15);
		}
		return label;
	}
	private JTextField getTit() {
		if (tit == null) {
			tit = new JTextField();
			tit.setColumns(10);
			tit.setBounds(89, 39, 333, 21);
		}
		return tit;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("비밀번호 :");
			label_1.setHorizontalAlignment(SwingConstants.CENTER);
			label_1.setBounds(172, 13, 63, 15);
		}
		return label_1;
	}
	private JTextField getPwd() {
		if (pwd == null) {
			pwd = new JTextField();
			pwd.setColumns(10);
			pwd.setBounds(234, 10, 79, 21);
		}
		return pwd;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("내용");
			lblNewLabel_1.setBounds(12, 56, 57, 15);
		}
		return lblNewLabel_1;
	}
	private JTextPane getResult() {
		if (result == null) {
			result = new JTextPane();
		}
		return result;
	}
}
